<template>
    <div>
        <h1>修改用药</h1>
        <table class="table table-bordered">
            <tbody>
                <tr>
                    <td>药品名称</td>
                    <td>
                        <input type="text" v-model="queryInfo.Mname">
                    </td>
                </tr>

                <tr>
                    <td>科室</td>
                    <td>
                        <select v-model="queryInfo.WardName">
                            <option value="">请选择</option>
                            <option :value="item.WardName" v-for="item in wardlist">{{ item.WardName }}</option>
                        </select>
                    </td>
                </tr>

                <tr>
                    <td>用药日期</td>
                    <td>
                        <input type="date" v-model="queryInfo.UseTime">
                    </td>
                </tr>

                <tr>
                    <td>病人姓名</td>
                    <td>
                        <input type="text" v-model="queryInfo.UserName">
                    </td>
                </tr>

                <tr>
                    <td>病房号码</td>
                    <td>
                        <input type="text" v-model="queryInfo.HouseNumber">
                    </td>
                </tr>

                <tr>
                    <td>用药状态</td>
                    <td>
                        <input type="radio" name="state" :value="1" v-model="queryInfo.UseState" checked>未完成
                        <input type="radio" name="state" :value="0" v-model="queryInfo.UseState">已完成
                    </td>
                </tr>

                <tr>
                    <td>配药医生</td>
                    <td>
                        <input type="text" v-model="queryInfo.DoctorName">
                    </td>
                </tr>

                <tr>
                    <td></td>
                    <td>
                        <input type="button" value="修改" class="btn btn-info" @click="ok">
                    </td>
                </tr>
            </tbody>
        </table>
    </div>
</template>

<script setup lang="ts">
import { ref, reactive, onMounted } from 'vue';
import axios from 'axios';
import { useRoute, useRouter } from 'vue-router';
import moment from 'moment';

//传递参数
const route = useRoute();
//跳转页面
const router = useRouter();

//声明一个变量接收前端传递的参数
let queryInfo: any = reactive({
    "MId": route.query.id,
    "Mname": "",
    "WardName": "",
    "UseTime": "",
    "UserName": "",
    "HouseNumber": "",
    "UseState": 1,
    "DoctorName": "",
    "IsDeleted": false
})


//声明一个变量来绑定科室
let wardlist: any = ref([])

//生命周期钩子
onMounted(() => {
    getWard()
    detail()
})

//绑定科室
const getWard = () => {
    axios({
        url: '/api/Medition/GetWard',
        method: 'get'
    })
        .then(res => {
            console.log(res);
            wardlist.value = res.data
        })
        .catch(err => {
            console.log(err);

        })
}

//反填
const detail = () => {
    axios({
        url: '/api/Medition/GetMedition',
        method: 'get',
        params: {
            id: route.query.id
        }
    })
}

//修改
const ok = () => {
    if (queryInfo.Mname == "") {
        alert('药品名称不能为空')
        return
    }
    if (queryInfo.UseTime == "") {
        alert('用药日期不能为空')
        return
    }
    if (queryInfo.UserName == "") {
        alert('病人姓名不能为空')
        return
    }
    if (queryInfo.HouseNumber == "") {
        alert('病房号码不能为空')
        return
    }
    if (queryInfo.DoctorName == "") {
        alert('配药医生不能为空')
        return
    }
    axios({
        url: '/api/Medition/Updasync',
        method: 'put',
        data: queryInfo
    })
        .then(res => {
            console.log(res);
            if (res.data == -1) {
                alert('药品名称不能重复')
                return
            }
            if (res.data > 0) {
                alert('修改成功')
                Object.assign(queryInfo, res.data[0])
                queryInfo.UseTime=res.data.UseTime.substring(0,10)
                router.push('/showMedition')
            }
            else {
                alert('修改失败')
                return
            }
        })
        .catch(err => {
            console.log(err);

        })
}
</script>

<style scoped></style>